package cn.com.RabitMQ;

import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

/**
 * @author liufl
 * @version 创建时间：2017年7月17日 下午6:13:59
 * 类说明
 */
public class ReceiveFanout {
	private final static String HOST = MQConfig.serverUrl;  
    private final static String EXCHANGE_NAME = "fanout";  
    private final static String QUEUE = "temp_fanout";  
    private final static String ROUTKEY = "mq.fanout";  
    private final static boolean DURABLE = true;  
   public static void main(String[] args) throws java.io.IOException,    
   java.lang.InterruptedException, TimeoutException{  
       // 创建连接和频道    
       ConnectionFactory factory = new ConnectionFactory();    
       factory.setHost(HOST);    
       Connection connection = factory.newConnection();    
       Channel channel = connection.createChannel();    
   
       channel.exchangeDeclare(EXCHANGE_NAME, "fanout");   
       //持久化  
       channel.queueDeclare(QUEUE, DURABLE, false, false, null);  
       channel.basicQos(1);  
       // 创建一个非持久的、唯一的且自动删除的队列    
       //String queueName = channel.queueDeclare().getQueue();    
       // 为转发器指定队列，设置binding    
       channel.queueBind(QUEUE, EXCHANGE_NAME,ROUTKEY);    
   
       System.out.println(" [*] Waiting for messages. To exit press CTRL+C");    
   
       QueueingConsumer consumer = new QueueingConsumer(channel);    
       // 指定接收者，第二个参数为自动应答，无需手动应答    
       channel.basicConsume(QUEUE, true, consumer);    
   
       while (true)    
       {    
           QueueingConsumer.Delivery delivery = consumer.nextDelivery();    
           String message = new String(delivery.getBody());    
           System.out.println(" [x] Received '" + message + "'");    
   
       }    
   }  
}

